import 'package:json_annotation/json_annotation.dart';
import '../utils/timestamp_utils.dart';

part 'user_model.g.dart';

@JsonSerializable()
class User {
  @JsonKey(name: 'id')
  final int id;

  @JsonKey(name: 'name')
  final String name;

  @JsonKey(name: 'username')
  final String username;

  @JsonKey(name: 'email')
  final String email;

  @JsonKey(name: 'phone')
  final String phone;

  @JsonKey(name: 'website')
  final String website;

  // 地址信息
  @JsonKey(name: 'address')
  final Address? address;

  // 公司信息
  @JsonKey(name: 'company')
  final Company? company;

  // 自定义字段
  @JsonKey(
      name: 'createdAt',
      fromJson: TimestampUtils.fromJson,
      toJson: TimestampUtils.toJson,
      defaultValue: null
  )
  final DateTime? createdAt;

  @JsonKey(
      name: 'updatedAt',
      fromJson: TimestampUtils.fromJson,
      toJson: TimestampUtils.toJson,
      defaultValue: null
  )
  final DateTime? updatedAt;

  User({
    required this.id,
    required this.name,
    required this.username,
    required this.email,
    required this.phone,
    required this.website,
    this.address,
    this.company,
    this.createdAt,
    this.updatedAt,
  });

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
  Map<String, dynamic> toJson() => _$UserToJson(this);

  // 复制方法，用于更新用户信息
  User copyWith({
    int? id,
    String? name,
    String? username,
    String? email,
    String? phone,
    String? website,
    Address? address,
    Company? company,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) {
    return User(
      id: id ?? this.id,
      name: name ?? this.name,
      username: username ?? this.username,
      email: email ?? this.email,
      phone: phone ?? this.phone,
      website: website ?? this.website,
      address: address ?? this.address,
      company: company ?? this.company,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }

  // 获取头像URL（基于用户名的哈希）
  String get avatarUrl {
    final hash = _generateHash(username);
    return 'https://www.gravatar.com/avatar/$hash?d=identicon';
  }

  // 生成简单的哈希值
  int _generateHash(String input) {
    return input.codeUnits.fold(0, (int previous, int element) {
      return (previous << 5) - previous + element;
    }).abs();
  }

  // 显示名称（优先使用name，其次username）
  String get displayName {
    if (name.isNotEmpty) return name;
    return username;
  }
}

@JsonSerializable()
class Address {
  @JsonKey(name: 'street')
  final String street;

  @JsonKey(name: 'suite')
  final String suite;

  @JsonKey(name: 'city')
  final String city;

  @JsonKey(name: 'zipcode')
  final String zipcode;

  @JsonKey(name: 'geo')
  final Geo? geo;

  Address({
    required this.street,
    required this.suite,
    required this.city,
    required this.zipcode,
    this.geo,
  });

  factory Address.fromJson(Map<String, dynamic> json) => _$AddressFromJson(json);
  Map<String, dynamic> toJson() => _$AddressToJson(this);

  // 格式化地址
  String get formattedAddress {
    return '$street, $suite, $city $zipcode';
  }
}

@JsonSerializable()
class Geo {
  @JsonKey(name: 'lat')
  final String lat;

  @JsonKey(name: 'lng')
  final String lng;

  Geo({required this.lat, required this.lng});

  factory Geo.fromJson(Map<String, dynamic> json) => _$GeoFromJson(json);
  Map<String, dynamic> toJson() => _$GeoToJson(this);
}

@JsonSerializable()
class Company {
  @JsonKey(name: 'name')
  final String name;

  @JsonKey(name: 'catchPhrase')
  final String catchPhrase;

  @JsonKey(name: 'bs')
  final String bs;

  Company({
    required this.name,
    required this.catchPhrase,
    required this.bs,
  });

  factory Company.fromJson(Map<String, dynamic> json) => _$CompanyFromJson(json);
  Map<String, dynamic> toJson() => _$CompanyToJson(this);

  // 公司简写
  String get shortName {
    if (name.length <= 15) return name;
    return '${name.substring(0, 12)}...';
  }
}